package com.groupfour.attendancemanagementsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.dao.StudentDao;
import com.groupfour.attendancemanagementsystem.entity.DO.StudentDO;
import com.groupfour.attendancemanagementsystem.entity.DTO.CRUStudentDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.PersonalGroupMemberDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.PersonalInformationDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.*;
import com.groupfour.attendancemanagementsystem.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    /**
     * @author 卑微小峰
     * @param userCode
     * @return StudentDO
     * @describe 获取学生登录信息
     */
    public StudentDO getStudentByStudentCode(String userCode) {
        return studentDao.getStudentByStudentCode(userCode);
    }

    /**
     * @author 卑微小峰
     * @param newPwd,userCode
     * @return int
     * @describe 学生修改自己的密码
     */
    @Override
    public int updatePassword(String newPwd, String userCode) {
        return studentDao.updatePassword(newPwd,userCode);
    }


    /**
     * @author 扶永超
     * @param cruStudentDTO,classesCode
     * @return int
     * @describe 添加学生信息
     */
    @Override
    public int addStudent(CRUStudentDTO cruStudentDTO) {
        // 生成学生初始密码密码
        String studentPassword = cruStudentDTO.getStudentIdentity().substring(cruStudentDTO.getStudentIdentity().length() - 6);
        return studentDao.addStudent(cruStudentDTO,studentPassword);
    }

    @Override
    public int deleteStudent(String studentCode) {
        return studentDao.deleteStudent(studentCode);
    }

    @Override
    public int updateStudent(CRUStudentDTO cruStudentDTO,String studentCode) {
        // 获取修改时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time= df.format(new Date());
        Timestamp updateTime= Timestamp.valueOf(time);
        return studentDao.updateStudent(cruStudentDTO,studentCode,updateTime);
    }

    @Override
    public Map<String,Object> selectStudent(CRUStudentDTO cruStudentDTO, Integer pageNum, Integer pageSize) {

        Map<String,Object> map = new HashMap<>();

        //清理PageHelper缓存
        PageHelper.clearPage();
        //创建分页对象
        PageHelper.startPage(pageNum, pageSize);

        List<RStudentVO> studentVOList = studentDao.selectStudent(cruStudentDTO);

        //创建分页信息对象
        PageInfo<RStudentVO> pageInfo = new PageInfo<>(studentVOList);
        //将结果返回
        List<RStudentVO> result = pageInfo.getList();

        List<RStudentVO> resultVO = new ArrayList<>();
        for (RStudentVO rStudentVO : result) {
            RStudentVO vo = new RStudentVO();
            BeanUtils.copyProperties(rStudentVO,vo);
            resultVO.add(vo);
        }
        map.put("pageCount",pageInfo.getTotal());
        map.put("result",resultVO);

        return map;
    }



    /**
     * @author 卑微小峰
     * @param userCode
     * @return PersonalInformationDTO
     * @describe 获取个人信息
     */
    @Override
    public PersonalInformationDTO getPersonalInformation(String userCode) {
        return studentDao.getPersonalInformation(userCode);
    }


    /**
     * @author 卑微小峰
     * @param newPhone,userCode
     * @return int
     * @describe 修改学生手机号码
     */
    @Override
    public int updateStudentPhone(String newPhone, String userCode) {
        return studentDao.updateStudentPhone(newPhone,userCode);
    }


    /**
     * @author 卑微小峰
     * @param newIdentity,userCode
     * @return int
     * @describe 修改学生身份证号码
     */
    @Override
    public int updateStudentIdentity(String newIdentity, String userCode) {
        return studentDao.updateStudentIdentity(newIdentity,userCode);
    }

    /**
     * @author 章珑赢
     * @param userCode
     * @return List<PersonalGroupMemberVO>
     * @describe 查询实训组成员
     */
    @Override
    public List<PersonalGroupMemberVO> selectGroupMember(String userCode) {
        List<PersonalGroupMemberDTO>personalGroupMemberDTOS=studentDao.selectGroupCodeAndCourseCode(userCode);
        List<PersonalGroupMemberVO>personalGroupMemberVOS=studentDao.selectGroupMember(personalGroupMemberDTOS);
        return personalGroupMemberVOS;
    }


    /**
     * @author 章珑赢
     * @param userCode
     * @return List<ProjectScoreVO>
     * @describe 查询项目得分
     */
    @Override
    public List<ProjectScoreVO> SelectAllProjectScore(String userCode) {
        List<PersonalGroupMemberDTO>personalGroupMemberDTOS=studentDao.selectGroupCodeAndCourseCode(userCode);
        List<ProjectScoreVO> projectScoreVOList=studentDao.selectProjectScore(personalGroupMemberDTOS);
        return projectScoreVOList;
    }


    /**
     * @author 章珑赢
     * @param
     * @return List<GroupRankingVO>
     * @describe 查询小组排行榜
     */
    @Override
    public List<GroupRankingVO> selectGroupRanking(String courseCode) {
        return studentDao.selectRanking(courseCode);
    }

    @Override
    public int updateStudentPhoto(String savePath, String userCode) {
        return studentDao.updatePhoto(savePath,userCode);
    }
}
